package coe.pumbaa
import coe.pumbaa.Post
import coe.pumbaa.Category

class PumbaaTagLib {
    static namespace = "pumbaa"
    
    private def navList = [
        //home
        'home':[text:'Home',controller:'home'],
        
        //home>category
        'category':[text:'Categories',controller:'category', under:'home'],
        //home>category>post
        'post':[text:'Posts',controller:'post', action:'list', under:'category', params:[get_id_from:"categoryID"]],
        //home>category>post>...
        'post_title':[under:'post', controller:'comment', action:'list', params:[get_id_from:"postID"]],
       
        //home>user
        'user':[text:'User',controller:'user',under:'home'],
        //home>user>subscription
        'subscription':[text:'Subscriptions',under:'user'],
        
        //home>calendar
        'calendar':[text:'Calendar', controller:'event',under:'home'],
        
        //home>admin
        'admin':[text:'Admin',controller:'admin', under:'home'],
        //home>admin>manage_user
        'manage_user':[text:'Manage Users',controller:'user',under:'admin'], //correct
        //home>admin>manage_role
        'manage_role':[text:'Manage Roles',controller:'role',under:'admin'], //corrent
        //home>admin>manage_category
        'manage_category':[text:'Manage Categories',under:'admin']
    ]
    
    def alert = { 
        if(flash.info){
            out << "<h4 class=\"alert_info\">" << flash.info << "</h4>"
        }else if(flash.warning){
            out << "<h4 class=\"alert_warning\">" << flash.warning << "</h4>"
        }else if(flash.error){
            out << "<h4 class=\"alert_error\">" << flash.error << "</h4>"
        }else if(flash.success){
            out << "<h4 class=\"alert_success\">" << flash.success << "</h4>"
        }
    }
    def navigation = { attrs, body ->
        if(attrs["navInstance"] != null && attrs["navInstance"].getClass() != java.util.ArrayList && attrs["navInstance"].pos != null){
            def nav = navList[attrs["navInstance"].pos]
            def navOut = ""
            def navText
            def navController
            def navAction
            def navParams
            def first = true

            //println attrs["navInstance"]
            out << "<div class=\"breadcrumbs_container\">"
            out << "<article class=\"breadcrumbs\">"
            while(nav){
                navParams = [:]     
                navText = { "..." }

                //controller
                if(first && attrs["navInstance"].controller != null && attrs["navInstance"].controller.compareTo("") != 0){
                    navParams['controller'] = attrs["navInstance"].controller                
                }else{
                    if(nav.controller != null && nav.controller.compareTo("") != 0){
                        navParams['controller'] = nav.controller
                    }
                }
                //action
                if(first && attrs["navInstance"].action != null && attrs["navInstance"].action.compareTo("") != 0){
                    navParams['action'] = attrs["navInstance"].action                
                }else{
                    if(nav.action != null && nav.action.compareTo("") != 0){
                        navParams['action'] = nav.action
                    }
                }
                //params
                if(first && attrs["navInstance"].params != null && attrs["navInstance"].params != []){
                    navParams['params'] = attrs["navInstance"].params
                }else{
                    if(nav.params != null && nav.params != []){
                        navParams['params'] = nav.params
                    }
                }
                if(nav.params != null && nav.params.get_id_from != null){
                    println attrs["navInstance"].params
                    println nav.params.get_id_from
                    println "-------------------"
                    navParams['params'].id = attrs["navInstance"].params[nav.params.get_id_from]
                }
                
                //text                  
                if(first && attrs["navInstance"].text != null && attrs["navInstance"].text.compareTo("") != 0){
                    navText = { attrs["navInstance"].text }
                }else{
                    if(nav.text != null && nav.text.compareTo("") != 0){
                        navText = { nav.text }
                    }

                }
                navOut = g.link(navParams,navText) + navOut
                if(nav["under"] == null || navList[nav.under] == null){
                    break
                }            
                navOut = "<div class=\"breadcrumb_divider\"></div>" + navOut
                nav = navList[nav.under]
                first = false
            }
            out << navOut
            out << "</article>"
            out << "</div>"
        }
    }
}

/*
<div class="breadcrumbs_container">
<article class="breadcrumbs">
<g:each in="${navigation}" var="bar" status="i">
<g:if test="${navigation.size()-(i+1) > 0}">
<g:link action="${navigationLink[i]}">${bar}</g:link><div class="breadcrumb_divider"></div>
</g:if><g:else>
<a class="current">${bar}</a>
</g:else>
</g:each>
</article>
</div>*/
